package com.tsxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsxy.entity.BookPictureRelation;
import com.tsxy.entity.UsedBookPictureRelation;
import com.tsxy.mapper.UsedBookPictureRelationMapper;
import com.tsxy.service.IUsedBookPictureRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 飞
 * @since 2022-03-12
 */
@Service
public class UsedBookPictureRelationServiceImpl extends ServiceImpl<UsedBookPictureRelationMapper, UsedBookPictureRelation> implements IUsedBookPictureRelationService {

    @Resource
    private UsedBookPictureRelationMapper relationMapper;
    @Override
    public int insertRelationBatch(Integer bookId, List<Integer> fileIds, Map<Integer,String> s) {
        QueryWrapper<UsedBookPictureRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id", bookId);
        relationMapper.delete(queryWrapper);

        // relationMapper.insertBatch(bookId, fileIds);
        List<UsedBookPictureRelation> relations = new ArrayList<>();
        for (Integer fileId : fileIds) {
            String name = s.get(fileId);
            relations.add(new UsedBookPictureRelation(bookId,fileId,name));
        }
        relationMapper.insertListBath(relations);
        return 1;
    }
}
